// 封装一些工具函数

// 根据 key 值获取 Storage
const getStorage = function(key) {
	let resData = '';
	uni.getStorage({
		key: key,
		success: function(res) {
			resData = res.data;
		}
	});
	return resData;
}

// 获取系统时间
const getDate = function(val) {
	//昨天的时间
	var day = new Date();
	switch (val) {
		case 'yesterday':
			return day.setTime(day.getTime() - 24 * 60 * 60 * 1000);
		case 'today':
			return day.setTime(day.getTime());
		case 'tomorrow':
			return day.setTime(day.getTime() + 24 * 60 * 60 * 1000);
	}
}

// 初始化音乐播放器
const musicInit = function(url, _this) {
	if (_this.innerAudioContext) {
		_this.innerAudioContext.destroy();
	}
	_this.innerAudioContext = uni.createInnerAudioContext()
	_this.innerAudioContext.autoplay = true;
	_this.innerAudioContext.src = url;
	_this.innerAudioContext.onPlay(() => {
		// console.log('开始播放');
	});
	_this.innerAudioContext.onPause(() => {
		// console.log('暂停播放');
	});
	_this.innerAudioContext.onStop(() => {
		// console.log('停止播放');
	});
	_this.innerAudioContext.onEnded(() => {
		console.log('音频自然播放结束事件');
		this.$store.commit('upDataPlayState', false);
	});
	_this.innerAudioContext.onTimeUpdate(() => {
		// console.log('音频播放进度更新事件');
	});
}
// 销毁当前音乐实例
const musicestroy = function(_this) {
	if (_this.innerAudioContext) {
		_this.innerAudioContext.destroy();
	}
}

export default {
	getStorage,
	getDate,
	musicInit,
	musicestroy
}
